# This test script performs the first of the benchmarks presented in
# the paper I am submitting to mobisys '10. The benchmark performs a
# sharpening, brightness adjustment, and contrast adjustment of the
# given input image using the specified scheduler. 
#
# The input images used in the paper can be found in testdata/percom.

from __future__ import with_statement
import sys
import scavenger
from time import sleep, time

# Check command line args.
if len(sys.argv) < 4 or not '-s' in sys.argv:
    print 'Usage: mobisys1.py imagefile -s scheduler [-i iterations]'
    sys.exit(1)

# Read in the image file.
with open(sys.argv[1], 'rb') as infile:
    image = infile.read()

# And also read in thumbnail version.
with open(sys.argv[1].replace('.jpg', '_thumb.jpg'), 'rb') as infile:
    thumb = infile.read()

# Find out which scheduler to use.
scheduler = sys.argv[sys.argv.index('-s') + 1]
if not scheduler in ('pc', 'tc', 'comb'):
    print 'Invalid scheduler. Valid values are pc, tc, and comb,'
    sys.exit(1)

# Check whether a number of test iterations is given.
iterations = 1
if '-i' in sys.argv:
    iterations = int(sys.argv[sys.argv.index('-i') + 1])

# Sleep for a little while to make sure that we discover
# the available surrogates.
sleep(2.0)

@scavenger.lprofilescavenge('len(#0)', store=True)
def lsharpen(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    factor = 1.0 + float(factor)
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.lprofilescavenge('len(#0)', store=True)
def lbrightness(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.lprofilescavenge('len(#0)')
def lcontrast(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Contrast(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.gprofilescavenge('len(#0)', store=True)
def gsharpen(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    factor = 1.0 + float(factor)
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.gprofilescavenge('len(#0)', store=True)
def gbrightness(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.gprofilescavenge('len(#0)')
def gcontrast(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Contrast(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store=True)
def csharpen(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    factor = 1.0 + float(factor)
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store=True)
def cbrightness(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)')
def ccontrast(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Contrast(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

functions = None
if scheduler == 'pc':
    functions = [lsharpen, lbrightness, lcontrast]
elif scheduler == 'tc':
    functions = [gsharpen, gbrightness, gcontrast]
else:
    functions = [csharpen, cbrightness, ccontrast]

# Run the test!
for x in range(0, iterations):
    start = time()
    try:
        for function in functions:
            result = function(image, 0.7)
    except Exception, e:
        print e.args
    stop = time()
    print stop - start

# Write the result for possible inspection.
with open('result.jpg', 'wb') as outfile:
    outfile.write(result)

scavenger.shutdown()
